package com.lhh.adminlte.service.impl;

import com.lhh.adminlte.dao.MemberMapper;
import com.lhh.adminlte.dao.QrcodeMapper;
import com.lhh.adminlte.entity.Member;
import com.lhh.adminlte.entity.MemberExample;
import com.lhh.adminlte.entity.Qrcode;
import com.lhh.adminlte.entity.QrcodeExample;
import com.lhh.adminlte.service.IQRCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: hanhui.liu
 * @Date: 2018/5/30 11:16
 * @Description:
 */
@Service
public class QRCodeServiceImpl implements IQRCodeService {

    @Autowired
    QrcodeMapper qrcodeMapper;

    @Autowired
    MemberMapper memberMapper;

    @Override
    public List<Qrcode> selectQrCodes(String batch, String value, String phone, Integer status, Date useDateStart, Date useDateEnd, Date createDateStart, Date createDateEnd, int displayStart, int displayLength) {
        QrcodeExample qrcodeExample = new QrcodeExample();
        qrcodeExample.setOffset(displayStart);
        qrcodeExample.setLimit(displayLength);
        QrcodeExample.Criteria criteria = qrcodeExample.createCriteria();
        criteria.andIsDeleteEqualTo(2);//未删除
        if (!StringUtils.isEmpty(value)) {
            criteria.andValueEqualTo(value);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andValueEqualTo(phone);
        }
        if (null != status && 0 != status) {
            criteria.andStatusEqualTo(status);
        }
        if (null != createDateStart) {
            criteria.andCreateTimeGreaterThanOrEqualTo(createDateStart);
        }
        if (null != createDateEnd) {
            criteria.andCreateTimeLessThanOrEqualTo(createDateEnd);
        }
        if (null != useDateStart) {
            criteria.andUseTimeGreaterThanOrEqualTo(useDateStart);
        }
        if (null != useDateEnd) {
            criteria.andUseTimeLessThanOrEqualTo(useDateEnd);
        }
        if (null != batch) {
            criteria.andBatchEqualTo(batch);
        }
        return qrcodeMapper.selectByExample(qrcodeExample);
    }

    @Override
    public long countQrCode(String batch, String value, String phone, Integer status, Date useDateStart, Date useDateEnd, Date createDateStart, Date createDateEnd) {
        QrcodeExample qrcodeExample = new QrcodeExample();
        QrcodeExample.Criteria criteria = qrcodeExample.createCriteria();
        criteria.andIsDeleteEqualTo(2);//未删除
        if (!StringUtils.isEmpty(value)) {
            criteria.andValueEqualTo(value);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andValueEqualTo(phone);
        }
        if (null != status && 0 != status) {
            criteria.andStatusEqualTo(status);
        }
        if (null != createDateStart) {
            criteria.andCreateTimeGreaterThanOrEqualTo(createDateStart);
        }
        if (null != createDateEnd) {
            criteria.andCreateTimeLessThanOrEqualTo(createDateEnd);
        }
        if (null != useDateStart) {
            criteria.andUseTimeGreaterThanOrEqualTo(useDateStart);
        }
        if (null != useDateEnd) {
            criteria.andUseTimeLessThanOrEqualTo(useDateEnd);
        }
        if (null != batch) {
            criteria.andBatchEqualTo(batch);
        }
        return qrcodeMapper.countByExample(qrcodeExample);
    }

    @Override
    public boolean addQrCode(String batch, int bonus, String value, Date createDate) {
        Qrcode qrcode = new Qrcode();
        qrcode.setBatch(batch);
        qrcode.setBonus(bonus);
        qrcode.setValue(value);
        qrcode.setCreateTime(createDate);
        qrcode.setStatus(1);//待使用
        qrcode.setIsDelete(2);//已删除
        qrcodeMapper.insert(qrcode);
        return true;
    }

    @Override
    public boolean deleteQrCode(int id) {
        QrcodeExample qrcodeExample = new QrcodeExample();
        qrcodeExample.createCriteria().andIdEqualTo(id);
        List<Qrcode> qrcodes = qrcodeMapper.selectByExample(qrcodeExample);
        if (qrcodes.size() == 1) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Qrcode qrcode = qrcodes.get(0);
            qrcode.setIsDelete(1);//删除
            qrcode.setPs("删除时间:" + simpleDateFormat.format(new Date()));
            qrcodeMapper.updateByPrimaryKey(qrcode);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateQrCode(int id, int status) {
        QrcodeExample qrcodeExample = new QrcodeExample();
        qrcodeExample.createCriteria().andIdEqualTo(id);
        List<Qrcode> qrcodes = qrcodeMapper.selectByExample(qrcodeExample);
        if (qrcodes.size() == 1) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Qrcode qrcode = qrcodes.get(0);
            if (qrcode.getStatus() == 2 || qrcode.getIsDelete() == 1) {
                //已使用或者已删除的兑换码不能更新
                return false;
            }
            if (status == 1 || status == 3) {
                qrcode.setStatus(status);
                qrcode.setPs("更新时间:" + simpleDateFormat.format(new Date()));
                qrcodeMapper.updateByPrimaryKey(qrcode);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean useQrCode(String value, String phone, Date useDate) {
        QrcodeExample qrcodeExample = new QrcodeExample();
        QrcodeExample.Criteria criteria = qrcodeExample.createCriteria();
        criteria.andValueEqualTo(value);
        List<Qrcode> qrcodes = qrcodeMapper.selectByExample(qrcodeExample);
        if (qrcodes.size() == 1) {
            Qrcode qrcode = qrcodes.get(0);
            if (qrcode.getStatus() == 1 && qrcode.getIsDelete() == 2) {
                //未删除且待使用
                qrcode.setUseTime(useDate);
                qrcode.setPhone(phone);
                qrcode.setStatus(2);
                qrcodeMapper.updateByPrimaryKey(qrcode);
                MemberExample memberExample = new MemberExample();
                memberExample.createCriteria().andPhoneEqualTo(phone);
                List<Member> members = memberMapper.selectByExample(memberExample);
                if (members.size() == 0) {
                    Member member = new Member();
                    member.setPhone(phone);
                    member.setStatus(1);
                    member.setBonus(qrcode.getBonus());
                    member.setCreateTime(useDate);
                    memberMapper.insert(member);
                } else {
                    Member member = members.get(0);
                    member.setBonus(member.getBonus().intValue() + qrcode.getBonus().intValue());
                    memberMapper.updateByPrimaryKey(member);
                }
                return true;
            }
        }
        return false;
    }

}
